package com.flmes.base.util;

import lombok.experimental.UtilityClass;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @Author: flmes
 * @Date: 2024/10/15 20:05
 */
@UtilityClass
public class FileUtil {

    /**
     *
     * @param file
     * @param path
     * @return
     * @throws IOException
     */
    public String saveFileToPath(MultipartFile file, String path) throws IOException {
        // 生成唯一的文件名
        String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
        Path filePath = Paths.get(path, fileName);

        // 保存文件
        try (InputStream inputStream = file.getInputStream()) {
            Files.copy(inputStream, filePath);
            return fileName;
        }
    }

}
